package com.example.springbean.component;

import com.example.springbean.model.Student;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BeanLifeComponent implements BeanNameAware {
    private Student student;
    public BeanLifeComponent() {
        System.out.println("执行构造函数...");
    }

    // setter 方法注入
    @Autowired
    public void setStudent(Student student) {
        this.student = student;
        System.out.println("执行 setter 方法...");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("执行 setBeanName 方法 " + name);
    }

    // 初始化
    @PostConstruct
    public void init() {
        System.out.println("初始化...");
    }

    // 使用
    public void use() {
        System.out.println("执行 use 方法...");
    }

    // 销毁前执行方法
    @PreDestroy
    public void destroy() {
        System.out.println("执行 destroy 方法...");
    }
}
